package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminDashboardService;
import com.cskaoyan.market.service.AdminDashboardServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketDashboardVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/dashboard")
public class AdminDashboardController extends HttpServlet {

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

        // String uri = req.getRequestURI();
        // String op = uri.replace(req.getContextPath() + "/admin/dashboard/", "");
        // if("info".equals(op)) {
        //     info(req,resp);
        // }


        // AdminDashboardService adminDashboardService = new AdminDashboardServiceImpl(req, resp);
        AdminDashboardService adminDashboardService = new AdminDashboardServiceImpl();
        MarketDashboardVo marketDashboardVo = adminDashboardService.dashboard();

        // ServletContext context = req.getServletContext();
        // Integer goodsTotal = (Integer) context.getAttribute("goodsTotal");
        // Integer userTotal = (Integer) context.getAttribute("userTotal");
        // Integer productTotal = (Integer) context.getAttribute("productTotal");
        // Integer orderTotal = (Integer) context.getAttribute("orderTotal");

        // System.out.println(goodsTotal);
        // System.out.println(userTotal);

        Map<String, Long> data = new HashMap();
        data.put("goodsTotal", marketDashboardVo.getGoodsTotal());
        data.put("userTotal", marketDashboardVo.getUserTotal());
        data.put("productTotal", marketDashboardVo.getProductTotal());
        data.put("orderTotal", marketDashboardVo.getOrderTotal());
        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);


    }

    private void info(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {

    }

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

    }
}
